import 'package:flutter/material.dart';
import 'package:honeypotflutter/public/views/public_keepAlive.dart';
import 'package:honeypotflutter/public/views/public_tabBar.dart';

class HLTabBarScrollView extends StatefulWidget {
  final List<String> mens;
  final List<Widget> children;
  final Function(int index)? indexAction;
  final EdgeInsetsGeometry? padding;
  final bool keepAlive;
  final double radius;

  HLTabBarScrollView({
    Key? key,
    required this.mens,
    required this.children,
    this.indexAction,
    this.padding,
    this.keepAlive = true,
    this.radius = 0.0,
  })  : assert((mens.isNotEmpty && children.isNotEmpty && (mens.length == children.length)), "mens和children不能为空"),
        super(key: key);

  @override
  State<HLTabBarScrollView> createState() => _HLTabBarScrollViewState();
}

class _HLTabBarScrollViewState extends State<HLTabBarScrollView> with SingleTickerProviderStateMixin {
  late TabController _controller = TabController(length: widget.mens.length, vsync: this);
  late HLTabBar tabbarWidget;
  late List<Widget> widgetChildren = [];

  @override
  void initState() {
    super.initState();
    tabbarWidget = HLTabBar(menuTitles: widget.mens, controller: _controller, radius: widget.radius);
    if (widget.keepAlive) {
      widgetChildren = widget.children.map((e) => HLKeepAlive(child: e)).toList();
    } else {
      widgetChildren = widget.children;
    }

    _controller.addListener(() {
      if (widget.indexAction != null) {
        widget.indexAction!(_controller.index);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        tabbarWidget,
        Expanded(
          child: (widget.padding == null)
              ? TabBarView(controller: _controller, children: widgetChildren)
              : Padding(
                  padding: widget.padding!,
                  child: TabBarView(controller: _controller, children: widgetChildren),
                ),
        ),
      ],
    );
  }
}
